package com.fhnew.app.ui.activity.userinfo.fragment.yue;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.model.internet.HttpAPI;
import com.fhnew.app.model.internet.HttpRequest;
import com.fhnew.app.model.internet.been.B_EconomyCount;
import com.fhnew.app.model.internet.been.B_RateList;
import com.fhnew.app.model.internet.been.B_YuEDetail;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.ui.activity.userinfo.FragmentDataChang;
import com.fhnew.app.ui.activity.userinfo.PageDataChangeControl;
import com.fhnew.app.util.DateUtil;
import com.fhnew.app.util.DoubleDatePickerDialog;
import com.fhnew.app.util.StringUtil;
import com.fhnew.app.widget.LoadingDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 资金明细
 */
public class CashListFragment extends Fragment implements FragmentDataChang {

    private PageDataChangeControl pageDataChangeControl;
    private String conentTag;

    @Override
    public void setListener (PageDataChangeControl pageDataChangeControl) {
        this.pageDataChangeControl = pageDataChangeControl;
    }

    @Override
    public void dataChanged (Bundle bundle) {
        conentTag = bundle.getString("conentTag");
        initInfoView();
    }

    //==============================================================
    // View
    //==============================================================

    private RadioGroup radioGroup;
    private RadioButton titleButton01, titleButton02, titleButton03;
    private Button timeAction;
    private RadioGroup timeGroup;
    private RadioButton timeButton03,timeButton04;
    private LinearLayout timeSearch;
    private TextView t_StartTime, t_EndTime;
    private Button check;

    private LinearLayout listLayout01, listLayout02, listLayout03;
    private RecyclerView listView01, listView02, listView03;
    private YuEDetailAdapter yuEDetailAdapter;
    private YuEEconomyAdapter yuEEconomyAdapter;
    private YuEEconomyRateAdapter yuEEconomyRateAdapter;

    private LoadingDialog loadingDialog;

    private void initView (View view) {
        radioGroup = view.findViewById(R.id.redioGroup_YuE_List);
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);

        titleButton01 = view.findViewById(R.id.redioButton_YuE_List_Item01);
        titleButton02 = view.findViewById(R.id.redioButton_YuE_List_Item02);
        titleButton03 = view.findViewById(R.id.redioButton_YuE_List_Item03);

        timeAction = view.findViewById(R.id.button_YuE_TimeAction);
        timeAction.setTag(false);
        timeAction.setOnClickListener(onClickListener);
        timeGroup = view.findViewById(R.id.redioGroup_YuE_Time01);
        timeGroup.setOnCheckedChangeListener(timeSelectedListener);
        timeButton03 = view.findViewById(R.id.redioButton_YuE_List_Time03);
        timeButton04 = view.findViewById(R.id.redioButton_YuE_List_Time04);
        timeSearch = view.findViewById(R.id.linearLayout_YuE_TimeSearchLayout);
        t_StartTime = view.findViewById(R.id.textView_YuE_StartTime);
        t_StartTime.setOnClickListener(onClickListener);
        t_EndTime = view.findViewById(R.id.textView_YuE_EndTime);
        t_EndTime.setOnClickListener(onClickListener);
        check = view.findViewById(R.id.button_YuE_Check);
        check.setOnClickListener(onClickListener);

        listLayout01 = view.findViewById(R.id.linearLayout_YuE_CashListContent01);
        listLayout02 = view.findViewById(R.id.linearLayout_YuE_CashListContent02);
        listLayout03 = view.findViewById(R.id.linearLayout_YuE_CashListContent03);

        listView01 = view.findViewById(R.id.recyclerView_YuE_Economy);
        listView01.setLayoutManager(new LinearLayoutManager(getContext()));
        yuEDetailAdapter = new YuEDetailAdapter(getContext());
        yuEDetailAdapter.setOnMoreActionListener(onMoreActionListener);
        listView01.setAdapter(yuEDetailAdapter);

        listView02 = view.findViewById(R.id.recyclerView_YuE_EconomyDetail);
        listView02.setLayoutManager(new LinearLayoutManager(getContext()));
        yuEEconomyAdapter = new YuEEconomyAdapter();
        yuEEconomyAdapter.setOnMoreActionListener(onMoreActionListener);
        listView02.setAdapter(yuEEconomyAdapter);

        listView03 = view.findViewById(R.id.recyclerView_YuE_Rate);
        listView03.setLayoutManager(new LinearLayoutManager(getContext()));
        yuEEconomyRateAdapter = new YuEEconomyRateAdapter();
        listView03.setAdapter(yuEEconomyRateAdapter);

        loadingDialog = new LoadingDialog((ViewGroup) view);
        loadingDialog.setDialogCancelable(false);
        loadingDialog.setShowTips(false);
    }

    private void initInfoView () {
        if (radioGroup == null) return;

        listLayout01.setVisibility(View.GONE);
        listLayout02.setVisibility(View.GONE);
        listLayout03.setVisibility(View.GONE);

        page = 1;
        switch (conentTag) {
            case "1":
                timeAction.setVisibility(View.VISIBLE);
                timeSearch.setVisibility(View.VISIBLE);
                check.setVisibility(View.VISIBLE);
                radioGroup.check(R.id.redioButton_YuE_List_Item01);
                timeGroup.check(R.id.redioButton_YuE_List_Time01);

                startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                endTime = startTime;

                listLayout01.setVisibility(View.VISIBLE);
                timeLayout(false);

                break;
            case "2":
                timeAction.setVisibility(View.VISIBLE);
                timeSearch.setVisibility(View.VISIBLE);
                check.setVisibility(View.INVISIBLE);
                radioGroup.check(R.id.redioButton_YuE_List_Item02);
                timeGroup.check(R.id.redioButton_YuE_List_Time01);

                startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                endTime = startTime;

                listLayout02.setVisibility(View.VISIBLE);
                timeLayout(false);
                break;
            case "3":
                timeAction.setVisibility(View.VISIBLE);
                timeSearch.setVisibility(View.INVISIBLE);
                check.setVisibility(View.INVISIBLE);
                radioGroup.check(R.id.redioButton_YuE_List_Item02);
                timeGroup.check(R.id.redioButton_YuE_List_Time02);

                startTime = DateUtil.time(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH)), -1);
                endTime = startTime;

                listLayout02.setVisibility(View.VISIBLE);
                timeLayout(true);
                break;
            case "4":
                radioGroup.check(R.id.redioButton_YuE_List_Item03);
                check.setVisibility(View.INVISIBLE);
                listLayout03.setVisibility(View.VISIBLE);
                if (yuEEconomyRate == null)
                    getYuEEconomyRate.startRequest(userModel.getToken());
                timeAction.setVisibility(View.INVISIBLE);
                timeLayout(false);
                break;
        }
    }

    private void timeLayout (boolean isShow) {
        timeAction.setTag(isShow);
        if (isShow) {
            if(conentTag.equals("1")){
                check.setVisibility(View.VISIBLE);
            }else{
                check.setVisibility(View.INVISIBLE);
            }
            timeGroup.setVisibility(View.VISIBLE);
            timeAction.setText("收起");
        } else {
            check.setVisibility(View.INVISIBLE);
            timeGroup.setVisibility(View.GONE);
            timeAction.setText("展开");
        }
    }

    //==============================================================
    // Control
    //==============================================================
    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.fragment_yue_cashlist, null);
        initView(container);
        initInfo();
        return container;
    }

    @Override
    public void onResume () {
        super.onResume();
        initInfoView();
    }

    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged (RadioGroup group, int checkedId) {
            page = 1;

            listLayout01.setVisibility(View.GONE);
            listLayout02.setVisibility(View.GONE);
            listLayout03.setVisibility(View.GONE);

            switch (checkedId) {
                case R.id.redioButton_YuE_List_Item01:
                    listLayout01.setVisibility(View.VISIBLE);
                    timeSearch.setVisibility(View.VISIBLE);
                    check.setVisibility(View.VISIBLE);
                    timeAction.setVisibility(View.VISIBLE);

                    timeButton03.setVisibility(View.VISIBLE);
                    timeButton04.setVisibility(View.VISIBLE);

                    conentTag = "1";
                    timeGroup.check(R.id.redioButton_YuE_List_Time01);
                    timeLayout(false);

                    startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                    endTime = startTime;
                    refreshInfo();
                    break;
                case R.id.redioButton_YuE_List_Item02:
                    listLayout02.setVisibility(View.VISIBLE);
                    timeAction.setVisibility(View.VISIBLE);
                    check.setVisibility(View.INVISIBLE);
                    timeSearch.setVisibility(View.INVISIBLE);

                    timeButton03.setVisibility(View.INVISIBLE);
                    timeButton04.setVisibility(View.INVISIBLE);

                    conentTag = "2";
                    timeGroup.check(R.id.redioButton_YuE_List_Time01);
                    timeLayout(false);

                    startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                    endTime = startTime;
                    refreshInfo();
                    break;
                case R.id.redioButton_YuE_List_Item03:
                    listLayout03.setVisibility(View.VISIBLE);
                    timeAction.setVisibility(View.INVISIBLE);
                    check.setVisibility(View.INVISIBLE);

                    if (yuEEconomyRate == null)
                        getYuEEconomyRate.startRequest(userModel.getToken());

                    conentTag = "4";
                    timeLayout(false);
                    break;
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener timeSelectedListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged (RadioGroup group, int checkedId) {
            page = 1;
            switch (checkedId) {
                case R.id.redioButton_YuE_List_Time01:
                    startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                    endTime = startTime;
                    refreshInfo();
                    break;
                case R.id.redioButton_YuE_List_Time02:
                    startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH) - 1);
                    endTime = startTime;
                    refreshInfo();
                    break;
                case R.id.redioButton_YuE_List_Time03:
                    endTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                    startTime = DateUtil.time(endTime, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
                    refreshInfo();
                    break;
                case R.id.redioButton_YuE_List_Time04:
                    startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + 1;
                    endTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
                    refreshInfo();
                    break;
            }
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            switch (v.getId()) {
                case R.id.button_YuE_TimeAction:
                    boolean timeShow = (Boolean) timeAction.getTag();
                    timeLayout(!timeShow);
                    break;
                case R.id.textView_YuE_StartTime:
                    timeCS = 1;
                    new DoubleDatePickerDialog(getContext(), 0, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), false).show();
                    break;
                case R.id.textView_YuE_EndTime:
                    timeCS = 2;
                    new DoubleDatePickerDialog(getContext(), 0, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), false).show();
                    break;
                case R.id.button_YuE_Check:
                    refreshInfo();
                    break;
            }
        }
    };

    private DoubleDatePickerDialog.OnDateSetListener onDateSetListener = new DoubleDatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet (DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {
            timeGroup.check(0);
            switch (timeCS) {
                case 1:
                    startTime = startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth;
                    t_StartTime.setText(startTime);
                    break;
                case 2:
                    endTime = startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth;
                    t_EndTime.setText(endTime);
                    break;
            }
        }
    };

    //资金明细列表
    private HttpAPI.GetYuEDetail getYuEDetail = new HttpAPI.GetYuEDetail() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest) {
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_YuEDetail result) {
            if (result.getCode() == 0) {
                yuEDetail = result;
                int type = 0;
                if (result.getPage() <= 1) {
                    type = 1;
                }else if (result.getData().size() < rows) {
                    type = 3;
                } else {
                    type = 2;
                }
                yuEDetailAdapter.setTypeBeens(result.getType());
                yuEDetailAdapter.notifyDataSetChanged(result.getData(), type);
            } else {
                Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
            }
            loadingDialog.dismiss();
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "资金明细获取失败", Toast.LENGTH_SHORT).show();
            loadingDialog.dismiss();
        }
    };

    //收益明细
    private HttpAPI.GetYuEEconomy getYuEEconomy = new HttpAPI.GetYuEEconomy() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest) {
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_EconomyCount result) {
            yuEEconomy = result;
            if (result.getCode() == 0) {
                int type = 0;
                if (result.getPage() <= 1) {
                    type = 1;
                }else if (result.getData().size() < rows) {
                    type = 3;
                } else {
                    type = 2;
                }
                yuEEconomyAdapter.notifyDataSetChanged(result.getData(),type);
            } else {
                Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
            }
            loadingDialog.dismiss();
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "收益明细获取失败", Toast.LENGTH_SHORT).show();
            loadingDialog.dismiss();
        }
    };

    private HttpAPI.GetYuEEconomyRate getYuEEconomyRate = new HttpAPI.GetYuEEconomyRate() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest) {
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_RateList result) {
            yuEEconomyRate = result;
            yuEEconomyRateAdapter.notifyDataSetChanged();
            loadingDialog.dismiss();
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "收益比率获取失败", Toast.LENGTH_SHORT).show();
            loadingDialog.dismiss();
        }
    };

    private RecyclerViewFoot.OnMoreActionListener onMoreActionListener = new RecyclerViewFoot.OnMoreActionListener() {
        @Override
        public void onMore () {
            page++;
            if(conentTag.equals("1")){
                if(yuEDetailAdapter.getDataType()!=3)
                    refreshInfo();
            }else{
                if(yuEEconomyAdapter.getDataType()!=3)
                    refreshInfo();
            }
        }
    };

    //===============================================================
    //  Model
    //===============================================================

    private UserModel userModel;
    private B_RateList yuEEconomyRate;
    private Calendar calendar;

    private B_YuEDetail yuEDetail;
    private B_EconomyCount yuEEconomy;
    private int page = 0;//资金明细页码


    private String startTime;
    private String endTime;
    private int rows = 10;
    private int timeCS = 1;

    private void initInfo () {
        calendar = Calendar.getInstance();
        userModel = UserModel.getInstance(getContext());

        startTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH));
        endTime = startTime;
    }


    private void refreshInfo () {
        t_StartTime.setText(startTime);
        t_EndTime.setText(endTime);
        if (conentTag.equals("1")) {
            getYuEDetail.startRequest(userModel.getToken(), startTime, endTime, page, rows, null, 0);
        } else {
            getYuEEconomy.startRequest(userModel.getToken(), startTime, endTime, page, rows);
        }
    }


    //===============================================================
    // 适配器
    //===============================================================
    private static class YuEDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private int selected = -1;
        private int dataType = -1; //  1 表示还有更多数据(首页数据)   2 表示还有更多数据(此页数据)   3 表示数据已经显示完成
        private List<B_YuEDetail.DataBean> dataBeans;
        private Context context;
        private List<B_YuEDetail.TypeBean> typeBeens;
        private RecyclerViewFoot.OnMoreActionListener onMoreActionListener;

        public YuEDetailAdapter (Context context) {
            this.context = context;
            dataBeans = new ArrayList<>();
        }

        public void setTypeBeens (List<B_YuEDetail.TypeBean> typeBeens) {
            this.typeBeens = typeBeens;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            View view = null;
            if (i == 1) {
                view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.item_yue_detile, viewGroup, false);
                return new YuEDetailViewHolder(view);
            } else {
                view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.item_recyclerview_foot, viewGroup, false);
                return new RecyclerViewFoot(view);
            }
        }

        public int getDataType () {
            return dataType;
        }

        @Override
        public int getItemViewType (int position) {
            if (position == dataBeans.size()) {
                return 2;
            } else {
                return 1;
            }
        }

        @Override
        public int getItemCount () {
            return dataBeans.size() + 1;
        }

        public void notifyDataSetChanged (List<B_YuEDetail.DataBean> dataBeans, int dataType) {
            this.dataType = dataType;
            switch (dataType) {
                case 1:
                    this.dataBeans.clear();
                    break;
                case 2:
                case 3:
                    break;
            }
            this.dataBeans.addAll(dataBeans);
            notifyDataSetChanged();
        }

        @Override
        public void onBindViewHolder (@NonNull RecyclerView.ViewHolder viewHolder, int i) {

            switch (viewHolder.getItemViewType()) {
                case 1:
                    YuEDetailViewHolder yuEDetailViewHolder = (YuEDetailViewHolder) viewHolder;
                    yuEDetailViewHolder.itemView.setTag(i);
                    B_YuEDetail.DataBean dataBean = dataBeans.get(i);

                    int color = 0;
                    if (i % 2 == 0) {
                        color = R.color.royalblue;
                    } else {
                        color = R.color.blue;
                    }

                    yuEDetailViewHolder.content01.setBackgroundResource(color);
                    yuEDetailViewHolder.content02.setBackgroundResource(color);
                    yuEDetailViewHolder.content03.setBackgroundResource(color);
                    yuEDetailViewHolder.content04.setBackgroundResource(color);
                    yuEDetailViewHolder.content05.setBackgroundResource(color);
                    yuEDetailViewHolder.content06.setBackgroundResource(color);

                    String type = null;
                    for (int j = 0; j < typeBeens.size(); j++) {
                        if (typeBeens.get(j).getId() == dataBean.getType()) {
                            type = typeBeens.get(j).getName();
                            break;
                        }
                    }

                    yuEDetailViewHolder.content01.setText(type);
                    yuEDetailViewHolder.content02.setText(dataBean.getOrderId());
                    yuEDetailViewHolder.content03.setText(dataBean.getChangeMoney());
                    yuEDetailViewHolder.content04.setText(dataBean.getMoney());
                    yuEDetailViewHolder.content05.setText(dataBean.getContent());
                    yuEDetailViewHolder.content06.setText(DateUtil.getFormatData(dataBean.getUpdated_at()));

                    StringUtil.format(yuEDetailViewHolder.content04);

                    yuEDetailViewHolder.orserView.setText(dataBean.getOrderId());

                    if (selected == i) {
                        yuEDetailViewHolder.orderLayout.setVisibility(View.VISIBLE);
                    } else {
                        yuEDetailViewHolder.orderLayout.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    RecyclerViewFoot recyclerViewFoot = (RecyclerViewFoot) viewHolder;

                    switch (dataType) {
                        case 1:
                        case 2:
                            recyclerViewFoot.button.setText("加载更多");
                            recyclerViewFoot.setOnMoreActionListener(onMoreActionListener);
                            break;
                        case 3:
                            recyclerViewFoot.button.setText("没有更多了");
                            break;
                    }
                    break;
            }
        }


        public class YuEDetailViewHolder extends RecyclerView.ViewHolder {
            public TextView content01, content02, content03, content04, content05, content06;

            public LinearLayout orderLayout;
            private TextView orserView;

            public YuEDetailViewHolder (@NonNull View itemView) {
                super(itemView);
                content01 = itemView.findViewById(R.id.textView_Item_YuEDetail01);
                content02 = itemView.findViewById(R.id.textView_Item_YuEDetail02);
                content03 = itemView.findViewById(R.id.textView_Item_YuEDetail03);
                content04 = itemView.findViewById(R.id.textView_Item_YuEDetail04);
                content05 = itemView.findViewById(R.id.textView_Item_YuEDetail05);
                content06 = itemView.findViewById(R.id.textView_Item_YuEDetail06);

                orderLayout = itemView.findViewById(R.id.linearLayout_Item_YuEDetail01);
                orserView = itemView.findViewById(R.id.textView_Item_YuEDetail08);

                itemView.setOnClickListener(onClickListener);
            }
        }


        private View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick (View v) {
                if (selected == (int) v.getTag()) {
                    selected = -1;
                } else {
                    selected = (int) v.getTag();
                }
                notifyDataSetChanged();
            }
        };

        public void setOnMoreActionListener (RecyclerViewFoot.OnMoreActionListener onMoreActionListener) {
            this.onMoreActionListener = onMoreActionListener;
        }

    }


    //=============================收益明细==============================================================

    private static class YuEEconomyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<B_EconomyCount.DataBean> dataBeans;
        private int dataType = -1; //  1 表示还有更多数据(首页数据)   2 表示还有更多数据(此页数据)   3 表示数据已经显示完成
        private RecyclerViewFoot.OnMoreActionListener onMoreActionListener;

        public YuEEconomyAdapter(){
            dataBeans = new ArrayList<>();
        }

        public int getDataType(){
            return dataType;
        }

        public void setOnMoreActionListener (RecyclerViewFoot.OnMoreActionListener onMoreActionListener) {
            this.onMoreActionListener = onMoreActionListener;
        }

        public void notifyDataSetChanged (List<B_EconomyCount.DataBean> dataBeans, int dataType) {
            this.dataType = dataType;
            switch (dataType){
                case 1:
                    this.dataBeans.clear();
                    break;
                case 2:
                case 3:
                    break;
            }
            this.dataBeans.addAll(dataBeans);
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            View view = null;
            if(i == 1){
                view = (ViewGroup) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_yue_rate, viewGroup, false);
                return new YuEEconomyViewHolder(view);
            }else{
                view = (ViewGroup) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recyclerview_foot, viewGroup, false);
                return new RecyclerViewFoot(view);
            }
        }

        @Override
        public void onBindViewHolder (@NonNull RecyclerView.ViewHolder viewHolder, int i) {

            switch (viewHolder.getItemViewType()){
                case 1:
                    YuEEconomyViewHolder yuEEconomyViewHolder = (YuEEconomyViewHolder) viewHolder;
                    B_EconomyCount.DataBean dataBean = dataBeans.get(i);
                    int color = 0;
                    if (i % 2 == 0) {
                        color = R.color.royalblue;
                    } else {
                        color = R.color.blue;
                    }

                    yuEEconomyViewHolder.content01.setBackgroundResource(color);
                    yuEEconomyViewHolder.content02.setBackgroundResource(color);

                    yuEEconomyViewHolder.content01.setText(dataBean.getCreated_at());
                    yuEEconomyViewHolder.content02.setText(dataBean.getChangeMoney());
                    break;
                case 2:
                    RecyclerViewFoot recyclerViewFoot = (RecyclerViewFoot) viewHolder;
                    switch (dataType) {
                        case 1:
                        case 2:
                            recyclerViewFoot.button.setText("加载更多");
                            recyclerViewFoot.setOnMoreActionListener(onMoreActionListener);
                            break;
                        case 3:
                            recyclerViewFoot.button.setText("没有更多了");
                            break;
                    }
                    break;
            }

        }


        @Override
        public int getItemViewType (int position) {
            if(position == dataBeans.size()){
                return 2;
            }
            return 1;
        }

        @Override
        public int getItemCount () {
            return dataBeans.size()+1;
        }

        public class YuEEconomyViewHolder extends RecyclerView.ViewHolder {

            private TextView content01, content02;

            public YuEEconomyViewHolder (@NonNull View itemView) {
                super(itemView);
                content01 = itemView.findViewById(R.id.textView_Item_Rate01);
                content02 = itemView.findViewById(R.id.textView_Item_Rate02);
            }
        }
    }


    private class YuEEconomyRateAdapter extends RecyclerView.Adapter<YuEEconomyRateAdapter.YuEEconomyRateViewHolder> {

        @NonNull
        @Override
        public YuEEconomyRateViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            View view = (ViewGroup) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_yue_rate, viewGroup, false);
            return new YuEEconomyRateViewHolder(view);
        }

        @Override
        public void onBindViewHolder (@NonNull YuEEconomyRateViewHolder yuEEconomyRateViewHolder, int i) {
            B_RateList.DataBean dataBean = yuEEconomyRate.getData().get(i);
            int color = 0;
            if (i % 2 == 0) {
                color = R.color.royalblue;
            } else {
                color = R.color.blue;
            }

            yuEEconomyRateViewHolder.content01.setBackgroundResource(color);
            yuEEconomyRateViewHolder.content02.setBackgroundResource(color);

            yuEEconomyRateViewHolder.content01.setText(dataBean.getColumn());
            yuEEconomyRateViewHolder.content02.setText(dataBean.getProportion());
        }

        @Override
        public int getItemCount () {
            if (yuEEconomyRate == null)
                return 0;
            return yuEEconomyRate.getData().size();
        }

        private class YuEEconomyRateViewHolder extends RecyclerView.ViewHolder {

            private TextView content01, content02;

            public YuEEconomyRateViewHolder (@NonNull View itemView) {
                super(itemView);
                content01 = itemView.findViewById(R.id.textView_Item_Rate01);
                content02 = itemView.findViewById(R.id.textView_Item_Rate02);
            }
        }
    }

    public static class RecyclerViewFoot extends RecyclerView.ViewHolder {
        public Button button;
        private OnMoreActionListener onMoreActionListener;

        public RecyclerViewFoot (@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button_Item_RecyclerViewFoot);
            button.setOnClickListener(onClickListener);
        }

        private View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                if (onMoreActionListener != null)
                    onMoreActionListener.onMore();
            }
        };

        public void setOnMoreActionListener (OnMoreActionListener onMoreActionListener) {
            this.onMoreActionListener = onMoreActionListener;
        }

        public interface OnMoreActionListener {
            public void onMore ();
        }
    }
}
